package com.xiaoyu.client.ui.activity.main.mine.my_info;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.mine.AddressBean;
import com.xiaoyu.client.model.mine.ParticularsBean;
import com.xiaoyu.client.network.NetworkManager;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.ui.adapter.AllPowerfulAdapter;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author jx
 * @date 创建时间 2018/10/15
 * @Description 选择地址
 */
public class AddressActivity extends BaseActivityWithTitle {
    public static final String ADDRESS_ID = "address_id";
    public static final String ADDRESS_DETAILS = "address_details";
    public static final String ADDRESS_TEL = "address_tel";
    @BindView(R.id.activity_address_recycler_view)
    RecyclerView mRecyclerView;

    private AllPowerfulAdapter mAdapter;
    private List<AddressBean.DataBean> mAddressList = new ArrayList<>();
    private String address, reciverTel,addressId;
    private TextView mRightTxt;

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("地址");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_address, parent, true);
        ButterKnife.bind(this);
        addRightTxt();
        initView();
    }

    //添加右侧文字
    private void addRightTxt() {
        mRightTxt = new TextView(this);
        mRightTxt.setText("确认");
        mRightTxt.setTextColor(Color.parseColor("#2796f0"));
        mRightTxt.setOnClickListener(this);
        addRightView(mRightTxt, 0);
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void onResume() {
        super.onResume();
        mAddressList.clear();
        getAddressInfo();
    }

    private void getAddressInfo() {
        mAddressList.clear();
        showLoading();
        NetworkManager.getPersonalAddress(new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                dissmissLoading();
                succeedResult(result);
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
                dissmissLoading();
            }
        });
    }

    private void succeedResult(String result) {
        if (result.length() == 0 || result == null) {
            return;
        }
        AddressBean bean = new Gson().fromJson(result, AddressBean.class);
        mAddressList.addAll(bean.getData());
        for (int i = 0; i < mAddressList.size(); i++) {
            if (bean.getData().get(i).getState().equals("1")) {//选中默认的
                address = bean.getData().get(i).getDetailed();
                reciverTel = bean.getData().get(i).getReceiver() + "   " + bean.getData().get(i).getTel();
                addressId = bean.getData().get(i).getAddressid();
            }
        }
        setMyAdapter(mAddressList);
    }

    private void setMyAdapter(final List<AddressBean.DataBean> mAddressList) {
        mAdapter = new AllPowerfulAdapter<AddressBean.DataBean>(R.layout.address_recycler_view_item, mAddressList) {
            @Override
            protected void convert(BaseViewHolder baseViewHolder, final AddressBean.DataBean dataBean) {
                super.convert(baseViewHolder, dataBean);
                baseViewHolder.setText(R.id.activity_address_name, dataBean.getReceiver());//收件人姓名
                baseViewHolder.setText(R.id.activity_address_street, dataBean.getDetailed());//收货地址
                baseViewHolder.setText(R.id.activity_address_mobile, dataBean.getTel());//收件人对应手机号

                final ImageView mSelected = baseViewHolder.getView(R.id.activity_address_selected_img);
                mSelected.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mSelected.setImageResource(R.mipmap.icon_tuikuan_hong);
                        selectedAddress(dataBean.getAddressid());
                    }
                });

                if (dataBean.getState().equals("1")) {//选中  0==不选中
                    mSelected.setImageResource(R.mipmap.icon_tuikuan_hong);
                } else {
                    mSelected.setImageResource(R.mipmap.icon_tuikuan_hui);
                }

                //删除
                baseViewHolder.setOnClickListener(R.id.activity_address_deleted, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ToastUtil.showToast(getApplicationContext(), "删除成功");
                        deleteAddress(dataBean.getAddressid());
                    }
                });
                //编辑
                baseViewHolder.setOnClickListener(R.id.address_recycler_view_item_compile, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getApplicationContext(), ShippingAddressActivity.class);
                        intent.putExtra(ADDRESS_ID, dataBean.getAddressid());//地址id
                        startActivity(intent);
                    }
                });

            }
        };
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();

    }


    //新增收货地址
    @OnClick(R.id.activity_address_shipping_address)
    public void shippingAddressClick() {
        startActivity(new Intent(this, ShippingAddressActivity.class));
    }

    //删除地址
    private void deleteAddress(String addressId) {
        NetworkManager.deleteAddress(addressId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                getAddressInfo();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    //选择地址
    private void selectedAddress(String addressId) {
        NetworkManager.selectedAddress(addressId, new CommonHttpResponser() {
            @Override
            public void onSuccess(String result) {
                getAddressInfo();
            }

            @Override
            public void onFailed(String errorCode, String errorDiscription) {
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mRightTxt) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putString(ADDRESS_DETAILS, address);
            bundle.putString(ADDRESS_ID,addressId);
            bundle.putString(ADDRESS_TEL, reciverTel);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
